package com.fyp.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DataProvider {

	  // Database fields
	  private SQLiteDatabase database;
	  private MySQLiteHelper dbHelper;
	  private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
	      MySQLiteHelper.COLUMN_USERNAME,MySQLiteHelper.COLUMN_EMAIL,MySQLiteHelper.COLUMN_PASSWORD };
	 
	  public DataProvider(Context context) {
	    dbHelper = new MySQLiteHelper(context);
	  }

	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }

	  public void close() {
	    dbHelper.close();
	  }

	  public User createUser(String[] user) {
	    ContentValues values = new ContentValues();
	    Log.v("user", user[0]+"---"+user[1]);
	    values.put(MySQLiteHelper.COLUMN_EMAIL, user[1]);
	    values.put(MySQLiteHelper.COLUMN_USERNAME, user[0]);
	    values.put(MySQLiteHelper.COLUMN_PASSWORD, user[2]);
	    long insertId = database.insert(MySQLiteHelper.TABLE_USERS, null,
	        values);
	    Cursor cursor = database.query(MySQLiteHelper.TABLE_USERS,
	        allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
	        null, null, null);
	    if (cursor != null)
	    {
	        cursor.moveToFirst();
	    
	    User newUser = cursorToUser(cursor);
	    cursor.close();
	    return newUser;
	    
	    }
	    else
	    {
	    	return null;
	    }
	   
	  }
	  public boolean checkUserNamePass(User u) {
		  
		    Cursor c = database.rawQuery(
		    		"SELECT "+MySQLiteHelper.COLUMN_ID
		    		+" From "+ MySQLiteHelper.TABLE_USERS
		    		+" Where "+MySQLiteHelper.COLUMN_USERNAME+" = '"+u.getUserName()
		    		+"' AND "+MySQLiteHelper.COLUMN_PASSWORD+" = '"+u.getPassword()+"'", null);
		    //Cursor c = database.query(false, MySQLiteHelper.TABLE_USERS, allColumns, MySQLiteHelper.COLUMN_USERNAME+"=? AND "+MySQLiteHelper.COLUMN_PASSWORD+"=?", new String[]{u.getUserName(),u.getPassword()}, null, null, null, null);
		    Log.v("Authentication results count: ",""+c.getCount()+"---"+c );
		    if (c.getCount()>0)
		    {
		    	c.moveToFirst();
		    	Log.v("Authentication results: ",c.getString(0));

		        return true;
		    
		    }
		    else
		    {
		    	return false;
		    }
		   
		    
		  }
	  public User getUser(Long id) {
		  
		    Cursor cursor = database.query(MySQLiteHelper.TABLE_USERS,
		    		allColumns, MySQLiteHelper.COLUMN_ID + " = " + id , null,
		        null, null, null);
		    if (cursor != null)
		    {
		        cursor.moveToFirst();
		    
		    User newUser = cursorToUser(cursor);
		    cursor.close();
		    return newUser;
		    
		    }
		    else
		    {
		    	return null;
		    }
		   
		    
		  }
	  public void deleteUser(User u) {
	    long id = u.getId();
	    System.out.println("User deleted with id: " + id);
	    database.delete(MySQLiteHelper.TABLE_USERS, MySQLiteHelper.COLUMN_ID
	        + " = " + id, null);
	  }
	  public void deleteUser(String u,String p) {
		    
		   Log.v("Del Function with String: ","User deleted with Password: " + p);
		    database.delete(MySQLiteHelper.TABLE_USERS, MySQLiteHelper.COLUMN_PASSWORD
		        + " = '" + p+"' And "+MySQLiteHelper.COLUMN_USERNAME+"= '"+u+"'", null);
		  }

	  public List<User> getAllUsers() {
	    List<User> users = new ArrayList<User>();

	    Cursor cursor = database.query(MySQLiteHelper.TABLE_USERS,
	        allColumns, null, null, null, null, null);
	    if(cursor.getCount()>0)
	    {
	    	cursor.moveToFirst();
	    	while (!cursor.isAfterLast()) 
	    	{
	    		
	    		users.add(cursorToUser(cursor));
	    		cursor.moveToNext();
	    	}
	    // Make sure to close the cursor
	    cursor.close();
	    return users;
	    }
	    else
	    	return null;
	   
	  }

	  private User cursorToUser(Cursor cursor) {
	    User u = new User();
	    u.setId(cursor.getLong(0));
	    u.setUserName(cursor.getString(1));
	    u.setEmail(cursor.getString(2));
	    u.setPassword(cursor.getString(3));
	    return u;
	  }
	} 
